<?php

declare(strict_types=1);

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
require_once __DIR__ . '/ExampleMiddlewareV2_1.php';
require_once __DIR__ . '/ExampleMiddlewareV2_2.php';

if (isset($app)) {
    // $app->add(new ExampleMiddlewareV2_1(app: $app));
    // $app->add(new ExampleMiddlewareV2_2($app->getResponseFactory(), $app));

    $app->add(ExampleMiddlewareV2_1::class);
    // $app->add(ExampleMiddlewareV2_2::class);// construct 需要参数， 实际没有给，报错
    // Add middleware to a route
    // $app->get('/', function () { ... })->add(new ExampleMiddlewareV2_1());

    // Add middleware to a route group
    // $app->group('/', function () { ... })->add(new ExampleMiddlewareV2_1());

    $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response, array $args) {
        $data = json_encode(['success' => 'ok']);
        $response->getBody()->write($data);
        return $response;
    });
}